package com.OrderingServer.Controller.seller;

import com.OrderingServer.Form.OrderForm;
import com.OrderingServer.convert.OrderForm2OrderDTOConverter;
import com.OrderingServer.dto.OrderDTO;
import com.OrderingServer.pojo.OrderMaster;
import com.OrderingServer.pojo.OverView;
import com.OrderingServer.pojo.ProductCategory;
import com.OrderingServer.pojo.ProductInfo;
import com.OrderingServer.service.IOrderMasterService;
import com.OrderingServer.service.ProductInfoService;
import com.OrderingServer.service.PushMessageService;
import com.OrderingServer.utils.Result;
import com.OrderingServer.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/seller/order-master")
public class OrderMasterController {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    IOrderMasterService orderMasterService;
    @Autowired
    PushMessageService pushMessageService;

        @ApiOperation("分页查询所有订单信息")
        @GetMapping("/selectPage")
        public Result getAllByPage(@RequestParam Integer pageNum,
                                   @RequestParam(defaultValue = "1000")Integer pageSize,
                                   @RequestParam String buyerName){
            PageInfo<OrderMaster> pageInfo =orderMasterService.selectPage(pageNum,pageSize,buyerName);
            return   Result.success(pageInfo);
        }
        @ApiOperation("新增餐饮类别")
        @PostMapping("/insert")
        public Result insert(@RequestBody OrderMaster orderMaster){
            return new Result().success(orderMasterService.save(orderMaster));
        }
        @ApiOperation("根据id更新餐饮类别")
        @PutMapping("/update")
        public Result update( @RequestBody OrderMaster orderMaster){
            return new Result().success(orderMasterService.updateById(orderMaster));
        }
        @ApiOperation("根据id删除餐饮类别")
        @DeleteMapping("delete/{id}")
        public Result delete( @PathVariable("id") String orderId){
            OrderMaster orderMaster =new OrderMaster(orderId,"1");
            return new Result().success(orderMasterService.updateById(orderMaster));
        }
    @RequestMapping("/overview")
    public Result overview() {
        LocalDateTime start = LocalDate.now().atStartOfDay();            // 00:00:00
        LocalDateTime end   = LocalDate.now().atTime(LocalTime.MAX);     // 23:59:59.999999999
        List<OrderMaster> todayOrderslist = orderMasterService.list(
                new QueryWrapper<OrderMaster>()
                        .between("create_time", start, end)   // create_time 改成你的字段
                        .orderByDesc("create_time")
        );
        Integer todayOrders= todayOrderslist.size();
        Integer activeUsers=orderMasterService.count(new QueryWrapper<OrderMaster>().select("distinct buyer_name"));
        Integer totalProducts=productInfoService.count(new QueryWrapper<ProductInfo>().select("product_name"));
        BigDecimal totalAmount = orderMasterService
                .getOne(new QueryWrapper<OrderMaster>()
                        .select("sum(order_amount) as order_amount")).getOrderAmount();
        OverView overView=new OverView(todayOrders,activeUsers,totalProducts,totalAmount);
        // 只查一列// 取出 sum 结果
    return new Result().success(overView);
        }
    /**
     * 卖家 取消订单
     */
    @GetMapping("/order/cancel")
    public Result cancelOrder(@RequestParam("openid") String openid,
                              @RequestParam("orderId") String orderId) {
        orderMasterService.buyercancel(openid,orderId);
        //消息推送
        List<OrderDTO> orderDTOList= orderMasterService.findlist(openid);
        for (OrderDTO orderDTO : orderDTOList) {
            if(orderDTO.getOrderId().equals(orderId)){
                //消息推送
                pushMessageService.orderStatus(orderDTO);
            }
        }

        //取消订单的业务逻辑
        return Result.success();
    }

}


